#!/usr/bin/env python 
#-*-encoding:utf8-*-

import sys
import numpy as np
from svm_Regression import SVM_Regression,prediction
from getData import *
from NonEssentialMethods import *
from RequiredMethods import *

def get_feature_dataSets(srcFile,mapDictFile,areaId):

    #************************************************src_dataset
    src_dataset = get_src_data(srcFile)
    #print src_dataset

    #************************************************
    #***************format src_file into dict*****************
    date_dict = format_srcDataset_to_dict(src_dataset)

    #for k in date_dict:
    #    print k,':',date_dict[k],'\n',
    #sys.exit()
    #*********************************************************

    #*****************area_ci map dict***********************
    list_dic = get_area_ci_dict(mapDictFile)
    map_keys = list_dic.keys()
    #print list_dic['101']

    #print list_dic
    #*******************************************************
    """
    selectable
    """
    #analysis_area_ci_map_size(list_dic)

    #------------------------------------------------------
    #sample specified dataset.
    sample_data_dict = sample_specified(date_dict,areaId)

    #----------------------------bak mid_dict
    bak_dict_pickle('../data/src_dict.pickle',sample_data_dict)

    #***************analysis sample dict structure******************
    #if need:
    #analysis_sample_dataset_dict(sample_data_dict)

    #-------------------------------------------------------------
    src_matrix = convert_srcdict_to_matrix(sample_data_dict,list_dic[str(areaId)],areaId)
    #*********************************************************
    #matrix is a dict. {'201501010000':[23,45,166,...],...}
    return src_matrix


def get_target_dataSets(targetFile):
    #************************************************
    target_dataset = get_target_data(targetFile)
    #print target_dataset
    _targetDict = format_targetDataset_to_dict(target_dataset)
    #print target_dict
    #sys.exit()
    return _targetDict


if __name__ == '__main__':
    
    #*****src datasets:
    feature_matrix = get_feature_dataSets('../data/dm_src_dataset.dat','../data/kyfx_map_area_ci.dat',101)
    test_feature_matrix = get_feature_dataSets('../data/dm_test_dataset.dat','../data/kyfx_map_area_ci.dat',101)

    target_dict = get_target_dataSets('../data/dm_target_values.dat')
    test_target_dict = get_target_dataSets('../data/dm_target_values_test.dat')

    #*************************join train dataset******************
    train_set_dict = join_train_dataSets(feature_matrix,target_dict,'../data/train_csv.csv')
    test_set_dict = join_train_dataSets(test_feature_matrix,test_target_dict,'../data/test_csv.csv') 
    #print test_feature_matrix
    #separate into corresponding XY.
    X,Y = separate_dataSets_into_XY(train_set_dict)
    tX,tY = separate_dataSets_into_XY(test_set_dict)
    #*************************************************
    
    #run model.
    model = SVM_Regression(X,Y)
    #print test_feature_matrix
    testDataSet = convert_dict_to_list(test_feature_matrix)
    pred_value = prediction(tX,model)
    print '\npredict results:------',len(pred_value),'-------\n',pred_value
    print '\nactual value: ----',len(tY),'-----\n',tY
    
    
    sys.exit()
    

    """
    #print type(tX)
    for records in tX:
        #print records
        pred_value = prediction(records,model)
        if pred_value <=0:
            pred_value = np.average(records)
        print pred_value,
    print '\n',
    print tY
    #pred_set_dict = join_test_dataSets(test_feature_matrix,,'pred_csv.csv')
    """
